package com.monkeygroup.mygoplus.common.imageview;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.view.home.activity.sell.PublishedActivity;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.Activity;

/**
 * 主界面MainActivity中的GridView的适配器
 */
public class ImageAdapter extends BaseAdapter {
    public static final Set<String> selectedImages = new HashSet<String>(); // 存储选中的图片的路径

    private String dirPath; // GridView中图片的父路径
    private List<String> imagePaths; // GridView中显示的图片的路径（s）
    private LayoutInflater inflater;

    private Activity activity;
    private String type="1";

    private int chooseNum =0;
    public ImageAdapter(Context context, List<String> data, String dirPath,String type) {
        this.dirPath = dirPath;
        this.imagePaths = data;
        this.type=type;
        inflater = LayoutInflater.from(context);
        activity=(Activity) context;
    }

    @Override
    public int getCount() {
        return imagePaths.size();
    }

    @Override
    public Object getItem(int position) {
        return imagePaths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sideworks_griditem, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.find_griditem_iv_image);
            viewHolder.checkBtn = (ImageButton) convertView.findViewById(R.id.find_griditem_ib_check);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 重置状态
       // viewHolder.imageView.setImageResource(R.mipmap.img_picture_selected);
//        viewHolder.checkBtn.setImageResource(R.mipmap.img_picture_selected);
        viewHolder.checkBtn.setVisibility(View.GONE);
        viewHolder.imageView.setColorFilter(null);
        // 加载图片
        ImageLoader.getInstance(3, ImageLoader.Type.LIFO).loadImage(dirPath + "/" + imagePaths.get(position), viewHolder.imageView);

        /**
         * 为GridView的Item设置点击事件
         */
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    String imagePath = dirPath + "/" + imagePaths.get(position);
                    if (selectedImages.contains(imagePath)) { // 如果图片已经被选择，则清楚选择状态
                        chooseNum--;
                        selectedImages.remove(imagePath);
                        viewHolder.imageView.setColorFilter(null);
                        viewHolder.checkBtn.setVisibility(View.GONE);
                        //     viewHolder.checkBtn.setImageResource(R.mipmap.img_picture_selected);
                    } else { // 如果图片没有被选择，则选择图片
                        int num=3;
                        if("1".equals(type)){
                            num=3;
                        }
                        else{
                            num=1;
                        }
                        if(chooseNum>num||chooseNum==num){
                            MyAlertDialog dialog = new MyAlertDialog(activity,"已達限制上傳數量","4");
                            dialog.show(activity,"已達限制上傳數量",4);
                        }
                        else{
                            chooseNum++;
                            selectedImages.add(imagePath);
                            viewHolder.checkBtn.setVisibility(View.VISIBLE);
                            viewHolder.imageView.setColorFilter(Color.parseColor("#77000000")); // 设置图片上面覆盖一层透明的黑色蒙版
                            //   viewHolder.checkBtn.setImageResource(R.mipmap.img_picture_selected); // 设置多选按钮为选中
                        }

                    }

            }
        });

        return convertView;
    }

    private class ViewHolder {
        ImageView imageView;
        ImageButton checkBtn;
    }
}


